package xcmg.device.web.basic;

import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import xcmg.device.infra.BasicController;
import xcmg.device.infra.ErrorCode;
import xcmg.device.service.basic.ModelMaterialService;
import xcmg.device.service.vo.FunctionResult;
import xcmg.device.service.vo.basic.MaterialsMaintenSearchVO;
import xcmg.device.service.vo.basic.MaterialsVO;
import xcmg.device.service.vo.basic.ModelMaterialRelationAddVO;
import xcmg.device.service.vo.basic.ModelMaterialRelationVO;
import yb.ecp.fast.infra.annotation.FastMappingInfo;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.infra.infra.PageCommonVO;
import yb.ecp.fast.infra.infra.SearchCommonVO;

import javax.servlet.http.HttpServletRequest;
import java.io.InputStream;
import java.util.Arrays;

/**
 * Created by wrg on 2019/5/28.
 */
@RequestMapping("/modelMaterial")
@RestController
public class ModelMaterialController extends BasicController {
    @Autowired
    private ModelMaterialService modelMaterialService;
/*
    @PostMapping(value = "/add")
    @FastMappingInfo(needLogin = true)
    @ApiOperation("新增机型与零件关系")
    public ActionResult add(@RequestBody ModelMaterialRelationAddVO addVO) throws Exception {
        if (null == addVO) {
            return actionResult(ErrorCode.IllegalArument);
        }

        ErrorCode errorCode = modelMaterialService.add(addVO, getOrgId());
        return actionResult(errorCode);
    }*/

    @PostMapping(value = "/delMaterials")
    @ApiOperation("删除保养包")
    @FastMappingInfo(needLogin = true)
    public ActionResult delMaterials(@RequestBody String[] ids) throws Exception {
        if (ids == null || ids.length == 0) {
            return actionResult(ErrorCode.IllegalArument);
        }
        return actionResult(modelMaterialService.delMaterials(Arrays.asList(ids)));
    }

    @PostMapping(value = "/submit")
    @FastMappingInfo(needLogin = true)
    @ApiOperation("新增，修改机型与零件关系")
    public ActionResult submit(@RequestBody ModelMaterialRelationAddVO addVO) throws Exception {
        if (null == addVO) {
            return actionResult(ErrorCode.IllegalArument);
        }

        ErrorCode errorCode = modelMaterialService.update(addVO, getOrgId());
        return actionResult(errorCode);
    }

    @PostMapping(value = "/associatedMaterialList")
    @ApiOperation("获取机型下已关联零件列表，机型id不能为空")
    @FastMappingInfo(needLogin = true)
    public ActionResult associatedMaterialList(@RequestBody SearchCommonVO<ModelMaterialRelationVO> condition) throws Exception {
        ModelMaterialRelationVO modelMaterialRelationVO = condition.getFilters();
        if (null == modelMaterialRelationVO) {
            modelMaterialRelationVO = new ModelMaterialRelationVO();
        }
        modelMaterialRelationVO.setOrgId(getOrgId());
        condition.setFilters(modelMaterialRelationVO);
        PageCommonVO pageCommonVO = modelMaterialService.associatedMaterialList(condition);
        return actionValueResult(pageCommonVO.getPageInfo());
    }

    @PostMapping(value = "/unrelatedMaterialList")
    @ApiOperation("获取机型下未关联零件列表，机型id不能为空")
    @FastMappingInfo(needLogin = true)
    public ActionResult unrelatedMaterialList(@RequestBody SearchCommonVO<ModelMaterialRelationVO> condition) throws Exception {
        ModelMaterialRelationVO modelMaterialRelationVO = condition.getFilters();
        if (null == modelMaterialRelationVO) {
            modelMaterialRelationVO = new ModelMaterialRelationVO();
        }
        modelMaterialRelationVO.setOrgId(getOrgId());
        condition.setFilters(modelMaterialRelationVO);
        PageCommonVO pageCommonVO = modelMaterialService.unrelatedMaterialList(condition);
        return actionValueResult(pageCommonVO.getPageInfo());
    }

    @PostMapping(value = "/queryMateList")
    @FastMappingInfo(needLogin = true)
    @ApiOperation("查询零件列表")
    public ActionResult queryMateList(@RequestBody SearchCommonVO<MaterialsMaintenSearchVO> searchVO) throws Exception {
        if (StringUtils.isEmpty(searchVO.getFilters().getOrgId())) {
            searchVO.getFilters().setOrgId(getOrgId());
        }
        PageCommonVO<MaterialsVO> result = modelMaterialService.queryMateList(searchVO);
        return actionValueResult(result.getPageInfo());
    }

    @RequestMapping(value = "/download",method = RequestMethod.POST)
    @ApiOperation("下载零件批量导入模板")
    @FastMappingInfo(needLogin = true)
    public void download() throws Exception {
        modelMaterialService.download();
    }


    @ResponseBody
    @RequestMapping(value = "/import", method = RequestMethod.POST)
    @ApiOperation("零件批量导入")
    @FastMappingInfo(needLogin = true)
    public ActionResult importExcel(HttpServletRequest request) throws Exception{
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        MultipartFile file = multipartRequest.getFile(multipartRequest.getFileNames().next());
        String fileName = file.getOriginalFilename();
        FunctionResult<String> result=new FunctionResult<>();
        if (!fileName.trim().endsWith(".xls")) {
            result.setCode(ErrorCode.ImportExcelEndWithXLS);
        } else {
            InputStream in = file.getInputStream();
            result= modelMaterialService.importExcel(in, getOrgId());
        }
        return actionResult(result.getCode(), result.getValue());
    }
}
